const app = getApp();

const http = require('../../../utils/wx_request').default


Page({
  data: {
    TabCur: 0,
    scrollLeft: 0,
    dates: [],
    times: [],
    makeList: []
  },
  onLoad: function (options) {
    let that = this;
    this.setData({
      dates: that.makeDate(),
      times: that.date()
    })
  },

  onShow() {
    let date = this.data.times[0].time
    this.dateList(date)
  },

  // 获取未来30天的日期数据
  makeDate(type) {
    let new_Date = new Date();
    let timesStamp = new_Date.getTime();
    let currenDay = new_Date.getDay();
    let dates = [];
    for (let i = 0; i < 30; i++) {
      dates.push({
        time: new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
          .toLocaleDateString()
          .replace(/[年月]/g, "")
          .replace(/[/]/g, "-"),
        day: (new Date(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - 1 - ((currenDay + 6) % 7)))).getDay() + 1)
      });
    }
    for (let i in dates) {
      let arr = [];
      if (dates[i].time[5] != 0 && dates[i].time[6] == '-') {
        for (let z in dates[i].time) {
          arr.push(dates[i].time[z])
        }
        arr[4] = 0;
        dates[i].time = arr.toString().replace(/[,]/g, '').slice(4);
      } else {
        dates[i].time = dates[i].time.slice(5);
      }
      if (dates[i].day == 1) {
        dates[i].day = '周一'
      } else if (dates[i].day == 2) {
        dates[i].day = '周二'
      } else if (dates[i].day == 3) {
        dates[i].day = '周三'
      } else if (dates[i].day == 4) {
        dates[i].day = '周四'
      } else if (dates[i].day == 5) {
        dates[i].day = '周五'
      } else if (dates[i].day == 6) {
        dates[i].day = '周六'
      } else if (dates[i].day == 7) {
        dates[i].day = '周日'
      }
    }
    return dates;
  },

  // Date
  date() {
    let new_Date = new Date();
    let timesStamp = new_Date.getTime();
    let currenDay = new_Date.getDay();
    let dates = [];
    for (let i = 0; i < 30; i++) {
      dates.push({
        time: new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - ((currenDay + 6) % 7)))
          .toLocaleDateString()
          .replace(/[年月]/g, "")
          .replace(/[/]/g, "-"),
        day: (new Date(new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - 1 - ((currenDay + 6) % 7)))).getDay() + 1)
      });
    }
    return dates
  },

  tabSelect(e) {
    let me = this.data
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    this.dateList(me.times[me.TabCur].time)
  },

  // 获取预约时间段列表
  dateList(date) {
    let result = {
      client_appid: wx.getStorageSync('appid'),
      date: date
    }
    http.timeList(result).then(res => {  
      let {
        data: {
          data
        }
      } = res
      this.setData({
        makeList: data
      })
    })
  },

  

  // 预约
  make() {
    wx.navigateTo({
      url: '/pages/index/affirmorder/affirmorder'
    })
  },

  // 预约时间列表
  timeList() {
    http.timeList()
  }
})
